<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

	<PropertyGroup>
		<MSBuildCommunityTasksPath>.</MSBuildCommunityTasksPath>
		<MSBuildMercurialPath>.</MSBuildMercurialPath>
		<OutputDirectory>build</OutputDirectory>
		<WebrootSource>.\Source\Jobping.ShortUrls</WebrootSource>
		<WebrootOutput>.\build\Release\Jobping.ShortUrls</WebrootOutput>
		<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
		<ProductVersion>9.0.21022</ProductVersion>
		<SchemaVersion>2.0</SchemaVersion>
	</PropertyGroup>

	<UsingTask TaskName="TransformXml" AssemblyFile="./Tools/Web/Microsoft.Web.Publishing.Tasks.Dll" />

	<Import Project=".\Tools\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

	<Import Project=".\Tools\MSBuild.Mercurial\MSBuild.Mercurial.Tasks" />
	
	<PropertyGroup>
		<Major>1</Major>
		<Minor>0</Minor>
		<Build>0</Build>
	</PropertyGroup>

	<Target Name="Clean">
		<RemoveDir Directories="$(OutputDirectory)" />
	</Target>
	
	<!-- Get Revision From HG -->
	<Target Name="Hg-Latest-Revision">
		<HgVersion LocalPath="$(MSBuildProjectDirectory)" Timeout="5000">
			<Output TaskParameter="Revision" PropertyName="AssemblyVersionRevision" />
		</HgVersion>
		<Message Text="Last revision from hg: $(AssemblyVersionRevision)"/>
	</Target>

	<!-- Just Compile -->
	<Target Name="Compile">
		<MakeDir Directories="$(OutputDirectory)" />
		<MSBuild Projects="Source\Jobping.ShortUrls.sln"
				 Properties="Configuration=Automated_Release;" />

		<AspNetCompiler
				TargetPath="$(OutputDirectory)\Release\Jobping.ShortUrls"
				 VirtualPath = "/Jobping.ShortUrls.csproj"
						PhysicalPath = ".\Source\Jobping.ShortUrls\"
						Force = "true"
						Debug = "false"
						Clean="true"
						Updateable = "true"
				/>

		<Delete Files="$(WebrootOutput)\Jobping.ShortUrls.csproj" />
		<Delete Files="$(WebrootOutput)\Jobping.ShortUrls.csproj.user" />
		<Delete Files="$(WebrootOutput)\Jobping.ShortUrls.Publish.xml" />
		<Delete Files="$(WebrootOutput)\App_Data\Db.mdf" />
		<Delete Files="$(WebrootOutput)\App_Data\Db_log.ldf" />

		<RemoveDir Directories="$(WebrootOutput)\obj" />
		<RemoveDir Directories="$(WebrootOutput)\Models" />
		<RemoveDir Directories="$(WebrootOutput)\Controllers" />

		<TransformXml Source="$(WebrootSource)\Web.config"
					Transform="$(WebrootSource)\Web.Release.config"
					Destination="$(WebrootOutput)\Web.config" />

		<Delete Files="$(WebrootOutput)\Web.Debug.config" />
		<Delete Files="$(WebrootOutput)\Web.Release.config" />

	</Target>

	<!-- Version the solution. (Get latest revision, then create the the version file)-->
	<Target Name="Version" DependsOnTargets="Hg-Latest-Revision;Version-File">
		<Message Text="Versioning Complete." />
	</Target>

	<!-- Update the Version File -->
	<Target Name="Version-File">
		<Message Text="Creating Version File: $(Major).$(Minor).$(Build).$(AssemblyVersionRevision)"/>
		<AssemblyInfo
			CodeLanguage="CS"
			OutputFile=".\Source\SolutionInfo.cs"
			AssemblyTitle="Jobping.ShortUrls"
			AssemblyDescription="The Jobping Url Shortener Website"
			AssemblyCompany="http://www.jobping.com/"
			AssemblyProduct="Jobping.ShortUrls"
			AssemblyCopyright="Copyright © whohive"
			ComVisible="false"
			CLSCompliant="true"
			Guid="53cf3a0e-d34b-4a48-bbcd-aecba50b339c"
			AssemblyVersion="$(Major).$(Minor).$(Build).$(AssemblyVersionRevision)"
			AssemblyFileVersion="$(Major).$(Minor).$(Build).$(AssemblyVersionRevision)"
			/>
	</Target>

	<!-- Zip File includes / excludes -->
	<ItemGroup>
		<SourceFileExclude Include="**\.hg\**" />
		<SourceFileExclude Include="build**" />
		<SourceFileExclude Include="**\.*" />
		<SourceFileExclude Include="**\bin\**" />
		<SourceFileExclude Include="**\obj\**" />
		<SourceFileExclude Include="**\Test\**" />
		<SourceFileExclude Include="**\TestResults\**" />
		<SourceFileExclude Include="**\*.user" />
		<SourceFileExclude Include="**\*.suo" />
		<SourceFileExclude Include="**\*.Db.mdf" />
		<SourceFileExclude Include="**\*.publish.xml" />
		<SourceFileExclude Include="**\*.cache" />
		<SourceFileExclude Include="**\*.vsmdi" />
		<SourceFileExclude Include="**\*.testsettings" />
	</ItemGroup>

	<ItemGroup>
		<SourceZipFiles Include="**\*.*" Exclude="@(SourceFileExclude)" />
	</ItemGroup>


	
	
	<!-- Version, Compile, Zip-Files-->
	<Target Name="Zip-Source" DependsOnTargets="Version;Compile;Zip-Install-Files;Zip-Source-Files">
	</Target>

	<Target Name="Zip-Source-Files">
		<Zip Files="@(SourceZipFiles)" ZipFileName=".\$(OutputDirectory)\Release\Jobping.ShortUrl_Source_$(Major).$(Minor).$(Build).$(AssemblyVersionRevision).zip" />
	</Target>

	<Target Name="Zip-Install-Files">
		<ItemGroup>
			<WebrootInstallFiles Include="$(WebrootOutput)\**" />
		</ItemGroup>
		<Zip Files="@(WebrootInstallFiles)" WorkingDirectory="$(WebrootOutput)"
			 ZipFileName=".\$(OutputDirectory)\Release\Jobping.ShortUrl_Install_$(Major).$(Minor).$(Build).$(AssemblyVersionRevision).zip" />
	</Target>

	<Target Name="Build" DependsOnTargets="Zip-Source">
		<Message Text="Jobping Build Complete"/>
	</Target>

</Project>